home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / Amiga_Mail_Vol2 / Archives / Plain / mj90 / ASCII / CLIWindowPtrII-93.ascii next >
Encoding:
Text File  |  1993-12-07  |  6.5 KB  |  183 lines

  1. (c) Copyright 1990 Commodore-Amiga, Inc.  All rights reserved.  The
  2. information contained herein is subject to change without notice, and
  3. is provided "as is" without warranty of any kind, either express or
  4. implied.  The entire risk as to the use of this information is assumed
  5. by the user. 
  6.  
  7. Using C to Get a Pointer 
  8. to the CLI Window
  9.  
  10. by Rob Wyesham
  11.  
  12. Programs sometime need to get a pointer to their CLI window.  A
  13.  
  14. method for obtaining this pointer was illustrated in the Amiga
  15. Mail article entitled "CLIColors.asm - Using ConsolePackets in
  16. 68000 Assembler" (page II-65, July/August 1989 Amiga Mail).
  17. However, many C programmers would rather avoid using assembly
  18. language.  This article shows how to use C to get a pointer the
  19. CLI window.
  20.  
  21. The basic method is the same whether you use C or Assembler.  You
  22. send an ACTION_DISK_INFO packet to the console handler process
  23. which includes a pointer to an InfoData struture. When the packet
  24. is returned by AmigaDOS, the id_VolumeNode field of the InfoData
  25. structure will be filled in with a pointer to the CLI window that
  26. the console handler is using.
  27.  
  28. The program below, WindowPtr.c, follows the code of CLIColors.asm
  29. closely.  Like the latter program, the strategy behind
  30. WindowPtr.c is to:
  31.  
  32. o Make sure the program was launched from a CLI.
  33. o Send an ACTION_DISK_INFO packet.
  34. o Extract the window pointer from the information 
  35.   returned in the packet.
  36.  
  37. The program starts by examining certain structures to determine
  38. if it was launched from a CLI.   If it passes that test, the
  39. program checks its Process structure to make sure that it has a
  40. valid pointer to a console task.  Next, the program allocates
  41. memory for FindWindow, a custom structure composed of a struct
  42. StandardPacket and a struct InfoData.  
  43.  
  44.  
  45. The program then initializes the StandardPacket fields and sends
  46. an ACTION_DISK_INFO packet to the DOS.  When the packet returns,
  47. the program checks for an error.  An error could possibly be
  48. caused by the CLI not having an associated window.  A CLI that
  49. uses the AUX: handler rather than the CON: handler could cause
  50. such an error.
  51.  
  52. If the packet returns without an error, the id_VolumeNode field
  53. of the InfoData structure points to the CLI window.  This is an
  54. ordinary C pointer, not a BPTR.  The  program shows that the
  55. pointer obtained does indeed point to the current CLI window by
  56. printing out a few of the window's parameters.
  57.  
  58. WindowPtr.c is listed below.  It was compiled under Lattice 5.05
  59. using the command:
  60.  LC -b1 -cfist -L -v -w WindowPtr.c.  Link with lc.lib and
  61. amiga.lib.
  62.  
  63. /*
  64.  * File name: windowptr.c
  65.  * Purpose: To illustrate how to get the pointer 
  66.  *                to the current CLI window.
  67.  * Requires: Must be started from a CLI
  68.  */
  69.  
  70. #include <libraries/dosextens.h>  /* Defines the StandardPacket
  71.                                    * structure, the packet types,
  72.                                    * and the Process structure
  73.                                    */
  74.  
  75. #include <libraries/dos.h>        /* Defines the InfoData structure */
  76.                                   /* and RETURN_XXX                 */
  77. #include <exec/types.h>
  78. #include <exec/memory.h>          /* Defines MEMF_XXX */
  79. #include <stdio.h>                /* Defines NULL and stdout */
  80. #include <stdlib.h>               /* Declares exit() */
  81. #include <proto/exec.h>
  82. #include <proto/intuition.h>
  83.  
  84.  
  85. /* Set up the FindWindow structure. */
  86. struct FindWindow
  87. {
  88.     struct StandardPacket FW_Pack;/* A StandardPacket is all long-words */
  89.     struct InfoData     FW_Info;  /* so InfoData is long-word-aligned   */
  90. };                                /* if the FindWindow struct is.       */
  91.  
  92.  
  93. #define THISTASK NULL
  94.  
  95. extern VOID cleanExit( struct FindWindow *, int );
  96.  
  97.  
  98. VOID main( int argc, char *argv[] )
  99. {
  100.     struct Process *myProcess = NULL;
  101.     struct FindWindow *thisWindow = NULL;
  102.     struct Window *addrThisWindow = NULL;
  103.     SHORT minW, minH;
  104.     USHORT maxW, maxH;
  105.     SHORT xPos, yPos, W, H;
  106.  
  107.     /* Get address of the current task */
  108.     myProcess = (struct Process *)FindTask( THISTASK );
  109.  
  110.     /* Make sure this program wasn't started from Workbench */
  111.     if ( argc == 0 ) cleanExit( thisWindow, RETURN_WARN );
  112.  
  113.     /* Make sure that there is a pr_ConsoleTask */
  114.     if ( myProcess->pr_ConsoleTask == NULL )
  115.         cleanExit( thisWindow, RETURN_WARN );
  116.  
  117.     /* Allocate memory for the FindWindow structure */
  118.     thisWindow = (struct FindWindow *)
  119.               AllocMem( sizeof( struct FindWindow ), MEMF_PUBLIC|MEMF_CLEAR );
  120.     if ( thisWindow == NULL ) cleanExit( thisWindow, RETURN_WARN );
  121.  
  122.     /* Initialize the packet */
  123.     thisWindow->FW_Pack.sp_Msg.mn_Node.ln_Name = 
  124.                                  (char *)&thisWindow->FW_Pack.sp_Pkt;
  125.     thisWindow->FW_Pack.sp_Pkt.dp_Link = (struct Message *)thisWindow;
  126.     thisWindow->FW_Pack.sp_Pkt.dp_Port = &myProcess->pr_MsgPort;
  127.     thisWindow->FW_Pack.sp_Pkt.dp_Type = ((LONG)&thisWindow->FW_Info) >> 2;
  128.  
  129.     /* Now send the packet, and wait for it to return */
  130.     PutMsg( (struct MsgPort *)myProcess->pr_ConsoleTask,
  131.                                     (struct Message *)thisWindow );
  132.  
  133.     (void)WaitPort( (struct MsgPort *) &myProcess->pr_MsgPort );
  134.  
  135.     /* Our program has been replied to, so get the reply message */
  136.     (void)GetMsg( &myProcess->pr_MsgPort );
  137.  
  138.     /* Save the address of the CLI window */
  139.     addrThisWindow = (struct Window *) (&(thisWindow->FW_Info))->id_VolumeNode;
  140.  
  141.  
  142.     /* If there was an error, terminate execution */
  143.     if ( thisWindow->FW_Pack.sp_Pkt.dp_Res1 == DOSFALSE )
  144.                 cleanExit( thisWindow, RETURN_WARN );
  145.  
  146.     if ( addrThisWindow == NULL ) cleanExit( thisWindow, RETURN_WARN );
  147.  
  148.     
  149.  
  150.  
  151.  
  152. /* Get the position, dimensions, and minimum and maximum
  153.  * dimensions of the currently active CLI window */
  154.     xPos = addrThisWindow->LeftEdge;
  155.     yPos = addrThisWindow->TopEdge;
  156.     W = addrThisWindow->Width;
  157.     H = addrThisWindow->Height;
  158.     minW = addrThisWindow->MinWidth;
  159.     minH = addrThisWindow->MinHeight;
  160.     maxW = addrThisWindow->MaxWidth;
  161.     maxH = addrThisWindow->MaxHeight;
  162.  
  163.  
  164.     /* Print the position, dimensions, and minimum and maximum
  165.      * dimensions of the currently active CLI window */
  166.     printf( "Position: x = %d, y = %d   ", xPos, yPos );
  167.     printf( "Dimensions: w = %d, h = %d\n", W, H );
  168.     printf( "%d <= width <= %d   ", minW, maxW );
  169.     printf( "%d <= height <= %d\n", minH, maxH );
  170.  
  171.     cleanExit( thisWindow, RETURN_OK );
  172. }
  173.  
  174.  
  175. VOID cleanExit( pktptr, returnValue )
  176. struct FindWindow *pktptr;
  177. int returnValue;
  178. {
  179.     if ( pktptr ) FreeMem( pktptr, (LONG)sizeof( struct FindWindow ) );
  180.  
  181.     exit( returnValue );
  182. }
  183.